﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace Cisoft.EMA
{
    public class SMS
    {
        private Cisoft.EMA.BLL.SMS_InboxBO inboxBO = new Cisoft.EMA.BLL.SMS_InboxBO();
        private Cisoft.EMA.BLL.MMS_OutboxBO outboxBO = new Cisoft.EMA.BLL.MMS_OutboxBO();
        private Cisoft.EMA.BLL.SMS_OutBox_BLL outboxBLL = new Cisoft.EMA.BLL.SMS_OutBox_BLL();

        public SMS()
        { 
        }

        #region 获取短信内容
        /// <summary>
        /// 从InBox表获取记录
        /// </summary>
        public DataSet GetListFromInBox()
        {
            DataSet ds = null;
            try
            {
                ds = inboxBO.GetAllRecord();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return ds;
        }

        /// <summary>
        /// 根据条件获取记录
        /// </summary>
        /// <param name="Where">条件</param>
        public DataSet GetListFromInBox(string Where)
        {
            DataSet ds = null;
            try
            {
                ds = inboxBO.GetList(Where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return ds;
        }

        // <summary>
        /// 根据应用程序编号获取记录
        /// </summary>
        /// <param name="Where">应用程序编号</param>
        public DataSet GetListFromInBoxByAppID(string AppID)
        {
            DataSet ds = null;
            try
            {
                ds = inboxBO.GetList(" ApplicationID = '" + AppID + "' ");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return ds;
        }
        #endregion

        #region 发送系统短信 public void SendMessage(string ShowNum,string Content)
        /// <summary>
        /// 发送系统短信,包含应用程序ID(AppID)
        /// </summary>
        /// <param name="ShowNum">手机号码</param>
        /// <param name="Content">短信内容</param>
        /// <param name="AppID">应用编号</param>
        public int SendMessage(string ShowNum, string Content,string AppID)
        {
            Cisoft.EMA.Model.SMS_OutboxMO _smsoutboxmo = new Cisoft.EMA.Model.SMS_OutboxMO();
            Cisoft.EMA.BLL.SMS_OutboxBO _smsoutboxbo = new Cisoft.EMA.BLL.SMS_OutboxBO();

            _smsoutboxmo.Extcode = "";
            _smsoutboxmo.Destaddr = ShowNum;
            _smsoutboxmo.Messagecontent = Content;
            _smsoutboxmo.Reqdeliveryreport = 1;
            //发送编码格式为gb2312
            _smsoutboxmo.Msgfmt = 15;
            _smsoutboxmo.Sendmethod = 0;
            _smsoutboxmo.Requesttime = GetEMATime().AddSeconds(3);
            _smsoutboxmo.Applicationid = AppID;

            return _smsoutboxbo.InsertRecord(_smsoutboxmo);

        }

        /// <summary>
        /// 发送系统短信,包含AppID和EcID
        /// </summary>
        /// <param name="ShowNum">手机号码</param>
        /// <param name="Content">短信内容</param>
        /// <param name="AppID">应用编号</param>
        /// <param name="EcID">EcID</param>
        public int SendMessage(string ShowNum, string Content, string AppID,string EcID)
        {
            Cisoft.EMA.Model.SMS_OutBox model_smsoutbox = new Cisoft.EMA.Model.SMS_OutBox();
            Cisoft.EMA.BLL.SMS_OutBox_BLL bll_smsoutbox = new Cisoft.EMA.BLL.SMS_OutBox_BLL();

            model_smsoutbox.Extcode = "";
            model_smsoutbox.Destaddr = ShowNum;
            model_smsoutbox.Messagecontent = Content;
            model_smsoutbox.Reqdeliveryreport = 1;
            //发送编码格式为gb2312
            model_smsoutbox.Msgfmt = 15;
            model_smsoutbox.Sendmethod = 0;
            model_smsoutbox.Requesttime = GetEMATime().AddSeconds(3);
            model_smsoutbox.Applicationid = AppID;
            model_smsoutbox.ECID = EcID;

            return bll_smsoutbox.InsertRecord(model_smsoutbox);

        }

        /// <summary>
        /// 发送系统短信,包含发送时间，AppID和EcID
        /// </summary>
        /// <param name="ShowNum">手机号码</param>
        /// <param name="Content">短信内容</param>
        /// <param name="Time">短信发送时间</param>
        /// <param name="AppID">应用程序编号编号</param>
        /// <param name="EcID">ECID</param>
        public int SendMessage(string ShowNum, string Content,DateTime Time, string AppID, string EcID)
        {
            Cisoft.EMA.Model.SMS_OutBox model_smsoutbox = new Cisoft.EMA.Model.SMS_OutBox();
            Cisoft.EMA.BLL.SMS_OutBox_BLL bll_smsoutbox = new Cisoft.EMA.BLL.SMS_OutBox_BLL();

            model_smsoutbox.Sismsid = System.Guid.NewGuid().ToString();
            model_smsoutbox.Extcode = "";
            model_smsoutbox.Destaddr = ShowNum;
            model_smsoutbox.Messagecontent = Content;
            model_smsoutbox.Reqdeliveryreport = 1;
            //发送编码格式为gb2312
            model_smsoutbox.Msgfmt = 15;
            model_smsoutbox.Sendmethod = 0;
            model_smsoutbox.Requesttime = Time;
            model_smsoutbox.Applicationid = AppID;
            model_smsoutbox.ECID = EcID;

            return bll_smsoutbox.InsertRecord(model_smsoutbox);

        }

        #endregion

        #region 读CanUpdate.config取配置文件 public string GetCanUpdateConfigValue(string Target)
        /// <summary>
        /// 读CanUpdate.config取配置文件
        /// </summary>
        /// <param name="Target"></param>
        /// <returns></returns>
        public string GetCanUpdateConfigValue(string Target)
        {
            string path = HttpContext.Current.Server.MapPath("~/xmlconfig/sys/CanUpdate.config");

            XmlDocument doc = loadConfigDocument(path);

            XmlElement root = doc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName("appSettings");

            //重新取得   节点名   
            XmlNode node = doc.SelectSingleNode("//" + "appSettings");
            if (node == null)
                throw new InvalidOperationException("appSettings" + "   section   not   found   in   config   file.");

            try
            {
                //   用   'add'元件   格式化是否包含键名     
                //   select   the   'add'   element   that   contains   the   key   
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", Target));
                string r = elem.Attributes["value"].Value.ToString();
                return r;
            }
            catch (Exception ex)
            {
                string ee = ex.Message.ToString();
                return "";
            }
        }
        #endregion

        #region 读入配置文件 public XmlDocument loadConfigDocument(string url)
        ///   <summary>   
        ///   读入配置文件   
        ///   </summary>   
        public XmlDocument loadConfigDocument(string url)
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(url);
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No   configuration   file   found.(没有找到配置文件)", e);
            }
        }
        #endregion

        #region 删除上行表sms_inbox的一条记录
        /// <summary>
        /// 删除上行表sms_inbox的一条记录
        /// </summary>
        /// <param name="EMASMSID">上行短信的ID</param>
        public void DeleteRecord(string EMASMSID)
        {
            inboxBO.Delete(EMASMSID);
        }
        #endregion

        #region 获取EMA服务器时间
        public DateTime GetEMATime()
        {
            return inboxBO.GetEMATime();
        }
        #endregion
    }
}
